package utils.common;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

public class SortUtil {

    private static String sss = "{" +
            "\"ab\":\"1\", " +
            "\"abcd\":\"1\", " +
            "\"ac\":\"1\", " +
            "\"abcde\":\"1\", " +
            "\"abcdef\":\"1\", " +
            "\"abc\":\"1\", " +
            "\"abcdeg\":\"1\", " +
            "\"abd\":\"1\", " +
            "\"abce\":\"1\", " +
            "\"abcdf\":\"1\", " +
            "\"a\":\"1\", " +
            "\"y\":\"1\" " +
            "}";

    public static void main(String args[]) {
        Map<String, Object> json = new TreeMap<>();
        paixu(sss, json);
        //List<String> list = new ArrayList<>();
        //list.add();
    }

    public static void paixu(String val, Map<String, Object> json) {
        JSONObject jsonObject = JSON.parseObject(val);

        int maxLength = 0;
        for (Map.Entry<String, Object> m : jsonObject.entrySet()) {
            maxLength = m.getKey().length();
        }
        List<char[]> keys = new ArrayList<>();
        for (Map.Entry<String, Object> m : jsonObject.entrySet()) {
            keys.add(m.getKey().toCharArray());
        }

        for (char[] key : keys) {
            System.out.println(key);
        }

        int keysLength = keys.size();

        for (int k=0; k<=maxLength; k++) {
            if (k > 30) {
                continue;
            }
            System.out.println(k);
            for (int i=0; i<keys.size(); i++) {
                for (int index=0; index<keysLength; index++) {
                    if (index + 1 == keys.size()) {
                        continue;
                    }
                    char[] ch1s = keys.get(index);
                    char[] ch2s = keys.get(index + 1);
                    if (ch1s.length < k || ch2s.length < k) {
                        continue;
                    }

                    char ch1 = ch1s.length == k ? 0 : ch1s[k];
                    char ch2 = ch2s.length == k ? 0 : ch2s[k];

                    if (k-1 < 0) {
                        if (ch1 > ch2) {
                            char[] ch0 = ch1s;
                            keys.set(index, ch2s);
                            keys.set(index + 1, ch0);
                        }
                    } else {
                        boolean deng = true;
                        for (int j=0; j<k; j++) {
                            char ch01 = ch1s[j];
                            char ch02 = ch2s[j];

                            if (ch01 != ch02) {
                                deng = false;
                                break;
                            }
                        }

                        if (deng && ch1 > ch2) {
                            char[] ch0 = ch1s;
                            keys.set(index, ch2s);
                            keys.set(index + 1, ch0);
                        }
                    }
                }
            }
            keysLength = keys.size();

            System.out.println("--------------------------------");
            for (char[] key : keys) {
                System.out.println(key);
            }
        }


    }
}
